package cn.tedu.medicalpath.attendance.service.impl;

import cn.tedu.medicalpath.attendance.mapper.AttendanceMapper;
import cn.tedu.medicalpath.attendance.pojo.dto.AttendanceQuery;
import cn.tedu.medicalpath.attendance.pojo.dto.AttendanceSaveParam;
import cn.tedu.medicalpath.attendance.pojo.entity.Attendance;
import cn.tedu.medicalpath.attendance.pojo.vo.AttendanceVO;
import cn.tedu.medicalpath.attendance.service.AttendanceService;
import cn.tedu.medicalpath.base.constant.AttendanceRecordStatus;
import cn.tedu.medicalpath.base.constant.OrderType;
import cn.tedu.medicalpath.base.exception.ServiceException;
import cn.tedu.medicalpath.base.response.StatusCode;
import cn.tedu.medicalpath.inspection.service.InspectionService;
import cn.tedu.medicalpath.order.service.AlipayService;
import cn.tedu.medicalpath.prescription.service.PrescriptionService;
import com.alipay.api.AlipayApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AttendanceServiceImpl implements AttendanceService {


    @Autowired
    AttendanceMapper attendanceMapper;
    @Autowired
    private InspectionService inspectionService;
    @Autowired
    private PrescriptionService prescriptionService;

    @Autowired
    AlipayService alipayService;

    @Override
    public List<AttendanceVO> select(AttendanceQuery attendanceQuery) {
        log.debug("开始处理【查询就诊记录】的业务，参数：{}",attendanceQuery);
        // 调用mapper层获取就诊记录列表
        List<AttendanceVO> list=attendanceMapper.select(attendanceQuery);
        //遍历每条就诊记录，将remark通过#$分为患者主诉和既往病史并set到对应的属性
        for (AttendanceVO attendanceVO : list) {
            String remark = attendanceVO.getRemark();
            String[] split = remark.split("#\\$");
            if(split.length>0){
                attendanceVO.setChiefComplaint(split[0]);
            }
            if(split.length>1){
                attendanceVO.setPastMedicalHistory(split[1]);
            }
        }
        return list;
    }


    @Override
    public void updateById(AttendanceSaveParam attendanceSaveParam) {
        log.debug("开始处理【更新就诊记录】的业务，参数：{}",attendanceSaveParam);
        Attendance attendance = new Attendance();
        attendance.setId(attendanceSaveParam.getId());
        attendance.setStatus(attendanceSaveParam.getStatus());
        log.debug("status：{}",attendance.getStatus());
        attendance.setRemark(attendanceSaveParam.getRemark());
        attendance.setUpdateTime(new Date());
        //修改检查单id和修改处方单id用于新增检查单和处方单时后端调用
        attendance.setInspectionId(attendanceSaveParam.getInspectionId());
        attendance.setPrescriptionId(attendanceSaveParam.getPrescriptionId());
        log.debug("修改中-【更新就诊记录】业务-入参：{},出参：{}",attendanceSaveParam,attendance);
        attendanceMapper.updateById(attendance);
    }

    @Override
    public void updateStatusById(Long id) {
        log.debug("开始处理【更新就诊状态】的业务，参数：{}",id);
        Attendance attendance = new Attendance();
        attendance.setId(id);
        attendance.setStatus("20");
        attendance.setUpdateTime(new Date());
        attendanceMapper.updateStatusById(attendance);
        log.debug("修改中-【更新就诊状态】业务-入参：{},出参：{}",id,attendance);

    }

    //新增就诊记录，调用插入空检查单和空处方单方法并获取到检查单和处方单id
    @Override
    @Transactional
    public Long insert(AttendanceSaveParam attendanceSaveParam) {
        log.debug("开始处理【新增就诊记录】的业务，参数：{}",attendanceSaveParam);
        Boolean isSuccess;
        try {
            isSuccess = alipayService.alipay(
                    attendanceSaveParam.getPatientId(),
                    OrderType.ATTENDANCE_RECORD,
                    attendanceSaveParam.getFee());
        } catch (AlipayApiException e) {
            throw new ServiceException(StatusCode.ALIPAY_FAILED);
        }
        if(!isSuccess){
            throw new ServiceException(StatusCode.ALIPAY_FAILED);
        }

        Attendance attendance = new Attendance();
        //使用copy
        BeanUtils.copyProperties(attendanceSaveParam,attendance);
        //设置创建时间
        attendance.setCreateTime(new Date());
        //设置状态为10
        attendance.setStatus(AttendanceRecordStatus.WAITING);
        long inspectionId =inspectionService.insertNull(attendanceSaveParam.getPatientId(), attendanceSaveParam.getDoctorId());
        attendance.setInspectionId(inspectionId);
        long prescriptionId =prescriptionService.insertNull(attendanceSaveParam.getPatientId(), attendanceSaveParam.getDoctorId());
        attendance.setPrescriptionId(prescriptionId);
        //前端挂号业务需要获取到就诊单id，所以这里返回id
        if(attendanceSaveParam.getRemark() == null || attendanceSaveParam.getRemark().isEmpty()){
            attendance.setRemark("#$#$");
        }
        attendanceMapper.insert(attendance);
        Long id = attendance.getId();
        log.debug("新增中-【新增就诊记录】业务-入参：{},出参：{}",attendanceSaveParam,id);
        return id;
    }
}
